package com.test.thread.threadOrder;

import java.util.concurrent.CountDownLatch;

public class ThreadDemo1 {

    public static void main(String[] args) {

        CountDownLatch countDownLatch1 = new CountDownLatch(0);

        CountDownLatch countDownLatch2 = new CountDownLatch(1);

        CountDownLatch countDownLatch3 = new CountDownLatch(1);


        Thread t1 = new Thread(new Work(countDownLatch1, countDownLatch2), "线程1");
        Thread t2 = new Thread(new Work(countDownLatch2, countDownLatch3), "线程2");
        Thread t3 = new Thread(new Work(countDownLatch3, countDownLatch3), "线程3");

        t1.start();
        t2.start();
        t3.start();
    }

    static class Work implements Runnable {
        CountDownLatch cOne;
        CountDownLatch cTwo;

        public Work(CountDownLatch cOne, CountDownLatch cTwo) {
            super();
            this.cOne = cOne;
            this.cTwo = cTwo;
        }

        @Override
        public void run() {
            try {
                cOne.await();
                System.out.println("执行: " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                cTwo.countDown();
            }
        }

    }

}
